<template>
    <div>
        <!-- 标题 -->
        <van-nav-bar title="登陆" />
        <!-- 表单 -->
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <!-- 手机号 -->
                <van-field v-model="username" name="username" left-icon="phone-o" placeholder="&ensp;手机号"
                    :rules="[{ pattern, message: '请填写手机号' }]">
                </van-field>
                <div style="height: 15px;"></div>
                <!-- 验证码 -->
                <van-field v-model="sms" name="sms" left-icon="todo-list-o" placeholder="&ensp;验证码"
                    :rules="[{ required: true, message: '请填写验证码' }]">
                    <!-- 插槽 -->
                    <template #button>
                        <van-button size="small" type="primary" @click="yan">{{ time > 0 ? time + '秒后获取' : '获取验证码'
                        }}</van-button>
                    </template>
                </van-field>
                <div style="height: 15px;"></div>
                <!-- 密码 -->
                <van-field v-model="password" :type="type" name="password" left-icon="lock" placeholder="&ensp;密&emsp;码"
                    :rules="[{ required: true, message: '请填写密码' }]">
                    <!-- 插槽 -->
                    <template #button>
                        <van-button style="border: none;" @click="click">
                            <van-icon name="eye-o" style="font-size: 25px;" />
                        </van-button>

                    </template>
                </van-field>
                <div style="height: 15px;"></div>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button block type="default" native-type="submit"
                    style="background-color: #ccc;border: none;color: #fff;">
                    登陆
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref } from 'vue';
const username = ref('');
const password = ref('');
// 手机号正则
let pattern = /^1[3-6]\d{9}$/;
// 明文密文切换
let type = ref('password')
const click = () => {
    // console.log(123);

    if (type.value == 'password') {
        type.value = 'text'
    } else {
        type.value = 'password'
    }
}

// 验证码倒计时禁用回显
let sms = ref('')
let time = ref(0)
let timer
const yan = () => {
    sms.value = Math.floor(Math.random() * 100000)

    if (time.value > 0) return
    time.value = 10
    timer = setInterval(() => {
        time.value--
        if (time.value == 0) {
            clearInterval(timer)
        }
    }, 1000)
}
import { useRouter } from 'vue-router';
const router = useRouter()
import { loginApi } from '@/api/api';
import { showFailToast, showSuccessToast } from 'vant';
const onSubmit = () => {
    loginApi(username.value, password.value).then((res) => {
        console.log(res);
        if (res.data.code == 0) {
            showSuccessToast('登陆成功')
            router.push('/home/index')
            localStorage.setItem('token', res.data.data.token)
        } else {
            showFailToast('登陆失败')
        }
    })
}
</script>

<style lang="scss" scoped></style>